﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.SuperLink.Util;
using Studio77.SuperLink.Core;
using System.Windows.Threading;

namespace Studio77.SuperLink
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static bool isShowOpenAd = false;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            AudioPlayHelper.PlayBackground("background.mp3");
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //清除页面堆栈
            try
            {
                NavigationService.RemoveBackEntry();
            }
            catch
            { }

            this.StoryboardMoving1.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardMoving2.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardMoving1.Begin();
            this.StoryboardMoving2.Begin();

            this.StoryboardTitle.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardTitle2.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardTitle.Begin();
            this.StoryboardTitle2.Begin();

            this.StoryboardFinger.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardFinger.Begin();

            if (StageManager.Instance.IsPlayBackground)
            {
                this.btnMusicOn.Visibility = System.Windows.Visibility.Visible;
                this.btnMusicOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnMusicOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnMusicOff.Visibility = System.Windows.Visibility.Visible;
            }

            if (StageManager.Instance.IsPlaySound)
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
            }

            String usemokaStr = UmengSDK.UmengAnalytics.GetOnlineParam("usemoka");
            //0-关闭 1-同时开启 2-开启banner 3-开启开屏
            if ((usemokaStr == "1" || usemokaStr == "3") && !isShowOpenAd)
            {
                isShowOpenAd = true;
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += (object ss, EventArgs ee) =>
                {
                    timer.Stop();
                    NavigationService.Navigate(new Uri("/Game77SharedLib;component/MokaPage.xaml?channel=fkllk", UriKind.RelativeOrAbsolute));
                };
                timer.Start();

            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.Click_Button);
            //导航到游戏类型选择页面
            NavigationService.Navigate(new Uri("/Pages/ClassifyPage.xaml", UriKind.Relative));
        }

        private void btnSoundOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	//关闭音效
            StageManager.Instance.IsPlaySound = false;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnMusicOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	//关闭音乐
            StageManager.Instance.IsPlayBackground = false;
            AudioPlayHelper.StopBackground();
            this.btnMusicOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnMusicOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnSoundOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //打开音效
            StageManager.Instance.IsPlaySound = true;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnMusicOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //打开音乐
            StageManager.Instance.IsPlayBackground = true;
            AudioPlayHelper.PlayBackground("background.mp3");
            this.btnMusicOn.Visibility = System.Windows.Visibility.Visible;
            this.btnMusicOff.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}